<?php
/**
 * 缓存插件
 */
class CachePlugin extends Yaf_Plugin_Abstract
{

	// public function routerStartup(Yaf_Request_Abstract $request, Yaf_Response_Abstract $response) {}

	/**
	 * 路由完成调用
	 * 页面缓存调用
	 * @method routerShutdown
	 * @param  Yaf_Request_Abstract  $request  [description]
	 * @param  Yaf_Response_Abstract $response [description]
	 * @return [type]                          [description]
	 * @author NewFuture
	 */
	public function routerShutdown(Yaf_Request_Abstract $request, Yaf_Response_Abstract $response)
	{
		$controller = $request->controller;
		$action     = $request->action;
		/*判断是否需要缓存*/
		switch ($action)
		{
			
			default:
			/*其他无缓存*/
				return;
		}
		/*读取缓存*/
		$cache_key = 'html_' . $cache_key;
		if ($cache = cache::get($cache_key))
		{
			/*缓存有效直接读取缓存*/
			exit($cache);
		}
		else
		{
			/*缓存标记设置*/
			/*cache.time.detail等记录缓存时间*/
			$cache_time = Yaf_Registry::get('config')['cache']['time'][$action];
			Yaf_Registry::set('cache_key', $cache_key);
			Yaf_Registry::set('cache_time', $cache_time);
			// exit($cache_key.$cache_time);
		}
	}

	// public function dispatchLoopStartup(Yaf_Request_Abstract $request, Yaf_Response_Abstract $response) {}
	// public function preDispatch(Yaf_Request_Abstract $request, Yaf_Response_Abstract $response) { }
	// public function postDispatch(Yaf_Request_Abstract $request, Yaf_Response_Abstract $response) {}

	/**
	 * 页面输出前调用
	 * 对于需要缓存的页面整页缓存
	 * @method dispatchLoopShutdown
	 * @param  Yaf_Request_Abstract  $request  [description]
	 * @param  Yaf_Response_Abstract $response [description]
	 * @return [type]                          [description]
	 * @author NewFuture
	 */
	public function dispatchLoopShutdown(Yaf_Request_Abstract $request, Yaf_Response_Abstract $response)
	{
		/*设置了页面缓存*/
		if (($key = Yaf_Registry::get('cache_key')) && ($time = Yaf_Registry::get('cache_time')))
		{
			cache::set($key, $response->getBody(), $time);
		}
	}
}